小编Ada*_*dam的帖子

redirect_to with:anchor.:锚点在Show动作上丢失,但在Create上运行正常

在我的Rails论坛应用程序中,当在主题中创建新帖子时,它将重定向到topic_path,其中包含页面索引和锚点的附加参数,以便滚动到帖子.像这样:

应用程序/控制器/ posts_controller.rb

def create
  @topic = Topic.find(params[:topic_id])
  @post = @topic.posts.build(post_params.merge({user_id: current_user.id}))

  if @post.save
    flash[:success] = "Post Created"
    redirect_to topic_path(@topic, :page => @post.page, :anchor => @post.anchor) 
  else
    render 'new'
  end
end
Run Code Online (Sandbox Code Playgroud)

重定向后的URL是: http://localhost:3000/topics/1?page=3#post-1364

但是我在Posts控制器的Show动作中做同样的事情.由于我不想自己显示帖子,因此该操作只需使用页面索引和帖子锚定重定向到主题.

应用程序/控制器/ posts_controller.rb

# Post are not displayed on their own. Showing one will jump to the post inside its topic
def show
  post = Post.find(params[:id])
  redirect_to topic_path(post.topic, :page => post.page, :anchor => post.anchor)
end
Run Code Online (Sandbox Code Playgroud)

但是为帖子调用show方法后的url不包含锚点.它确实包括页面:http://localhost:3000/topics/1?page=3 我调试了show方法,post.anchor正在被正确解析.

我的终端输出显示锚由于某种原因丢失

Started GET "/posts/1364" for 127.0.0.1 at 2014-08-13 10:03:31 …
Run Code Online (Sandbox Code Playgroud)

anchor redirect ruby-on-rails ruby-on-rails-4

11
推荐指数
1
解决办法
6101
查看次数

我无法使用accepts_nested_attributes_for创建模型对象.它不会创建嵌套对象

我的模型结构如下所示:

董事会has_many主题.主题has_many帖子.

应用程序/模型/ board.rb

class Board < ActiveRecord::Base
    has_many :topics
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ topic.rb

class Topic < ActiveRecord::Base
    belongs_to :user
    belongs_to :board
    has_many :posts

    accepts_nested_attributes_for :posts

    validates :title, presence: true, length: { maximum: 255 }
    validates :user_id, presence: true
    validates :board_id, presence: true
    ...
end
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ post.rb

class Post < ActiveRecord::Base
    belongs_to :user
    belongs_to :topic

    validates :user_id, presence: true
    validates :topic_id, presence: true
    validates :content, length: { minimum: 8 }
end
Run Code Online (Sandbox Code Playgroud)

以下是我创建新主题的观点.fields_for部分用于在新帖子上创建:内容

应用程序/视图/主题/ new.html.erb

<div>
    <%= form_for [@board, @topic] do |f| …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails associations nested-resources nested-attributes ruby-on-rails-4

3
推荐指数
1
解决办法
1847
查看次数

React - 为什么在这个例子中不需要绑定?

试图找出React的基础知识.

查看本页的第二个例子:https://facebook.github.io/react/ 我看到tick()函数设置Timer类的状态,将前一个值递增一.

class Timer extends React.Component {
  constructor(props) {
    super(props);
    this.state = {secondsElapsed: 0};
  }

  tick() {
    this.setState((prevState) => ({
      secondsElapsed: prevState.secondsElapsed + 1
    }));
  }

  componentDidMount() {
    this.interval = setInterval(() => this.tick(), 1000);
  }

  componentWillUnmount() {
    clearInterval(this.interval);
  }

  render() {
    return (
      <div>Seconds Elapsed: {this.state.secondsElapsed}</div>
    );
  }
}

ReactDOM.render(<Timer />, mountNode);
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试实现我自己的简单Counter类时,它失败了,我得到一个控制台错误,说无法读取未定义的属性setState.

class Counter extends React.Component {
    constructor(props) {
        super(props);
        this.state = {count: 0};
    }

    increment(prevState) {
        this.setState((prevState) => ({
            count: prevState.count + 1 …
Run Code Online (Sandbox Code Playgroud)

binding reactjs

3
推荐指数
1
解决办法
2213
查看次数