Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用局部视图';取而代之的是s控制器_Ruby On Rails_Ruby On Rails 3_Devise_Partial Views - Fatal编程技术网

Ruby on rails 使用局部视图';取而代之的是s控制器

Ruby on rails 使用局部视图';取而代之的是s控制器,ruby-on-rails,ruby-on-rails-3,devise,partial-views,Ruby On Rails,Ruby On Rails 3,Devise,Partial Views,我尝试使用局部视图的控制器,而不是渲染它的视图。我的设想如下: 我有用户,用户可以发布一些东西。这就是为什么我有两个控制器和模型(用户和帖子) 在用户页面中,有一个文本区域用于获取内容输入和发布按钮。我已经将这个视图../views/posts/_new.html.erb写成了部分视图,我在../views/users/index.html.erb中呈现了这个部分视图,为了使用部分视图,我使用了这段代码 <%= render 'posts/new' %> ../views/post

我尝试使用局部视图的控制器,而不是渲染它的视图。我的设想如下:

我有用户,用户可以发布一些东西。这就是为什么我有两个控制器和模型(用户和帖子)

在用户页面中,有一个文本区域用于获取内容输入和发布按钮。我已经将这个视图../views/posts/_new.html.erb写成了部分视图,我在../views/users/index.html.erb中呈现了这个部分视图,为了使用部分视图,我使用了这段代码

<%= render 'posts/new' %>
../views/posts/_new.html.erb

  def new
    @post = Post.new
  end

  def edit
    @post = Post.find(params[:id])
  end

  def create
    @user = User.find(params[:user_id])
    @post = @user.posts.create(post_params)

    if @post.save
      redirect_to root_path
    else
      render 'new'
    end
  end
<%= form_for :post do |f| %>
          <%= f.text_area :content, :class => "form-control", "rows" => "5", "id" => "comment", "placeholder" => "Write something to share with your mates ..." %><br/>
          <%= button_tag  "Share", :class => "btn btn-primary pull-right" do %>
            <span class="glyphicon glyphicon-share-alt"></span>
          <% end %>
        <% end %>

“表单控件”、“行”=>“5”、“id”=>“注释”、“占位符”=>“写一些东西与您的同伴共享…”%>
“btn btn主拉右”do%>
这里的问题是,当我在部分视图中单击按钮时,它会转到Deviate controller的用户注册控制器。为什么?


,guide告诉我们,如果partials和呈现它的视图之间存在关系,rails就会理解我们想要做什么,并使用partialview的控制器。我在这里遗漏了什么吗?

添加
:在
按钮标签中键入=>'submit'

   <%= form_for :post do |f| %>
      <%= f.text_area :content, :class => "form-control", "rows" => "5", "id" => "comment", "placeholder" => "Write something to share with your mates ..." %><br/>
      <%= button_tag  "Share", :type => "submit", :class => "btn btn-primary pull-right" do %>
        <span class="glyphicon glyphicon-share-alt"></span>
      <% end %>
    <% end %>

对你的表格要更明确

<%= form_for @post do |f| %>

问题是,在您当前的解决方案中,表单没有给出明确的url和方法,因此不知道您想用它创建帖子

它现在似乎要更正控制器,但给出了此错误
在PostsController中找不到具有'id'=
的用户#创建以下代码
@User=User.find(params[:User\u id])
,但是,在用户索引中进行
@post=post.new
类初始化不是错误的吗。您也可以只指定url和方法,但使用@post是可以的。For:user\u id您还需要将参数与表单一起传递…为什么它不能获取用户的id?好吧,您跳到另一个控制器,默认情况下,您的user\u id不会通过rails传递。您可以选择创建类似current_user(当前用户)的内容,以使用户id全局可访问,或者您只需在提交表单时传递用户id参数。
<%= form_for @post do |f| %>
@post = Post.new